package org.feng.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.feng.entity.Dept;
import org.feng.mapper.DeptMapper;
import org.feng.service.DeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author feng
 * @since 2021-08-27
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {
    @Resource
    private DeptMapper deptMapper;

    @Override
    public List<Dept> deptTree(Long deptId) {
        ArrayList<Dept> deptList = new ArrayList<>();
        Dept dept = deptMapper.selectById(deptId);
        deptList.add(dept);
        return deptTreeAgain(dept, deptList);
    }

    private List<Dept> deptTreeAgain(Dept dept, List<Dept> deptList) {
        List<Dept> list = deptMapper.selectList(Wrappers.<Dept>lambdaQuery().eq(Dept::getParentId, dept.getId()));
        deptList.addAll(list);
        for (Dept dept1 : list) {
            return deptTreeAgain(dept1,deptList);
        }
        return null;
    }
}
